// import axios from 'axios';
import { stringify } from 'qs';
import moment from 'moment';
import http from '../libs/http';

const dateFormat = 'YYYY-MM-DD HH:mm:ss';

export function login(params) {
    return http.post('/api/login?', params);
}

export function logout() {
    return http.post('/api/logout');
}

// ------------------------------------------------------------------------------------------- 用户管理

export function findUser(params) {
    return http.get(`/api/user?${stringify(params)}`);
}

export function getUser(username) {
    return http.get(`/api/user/${username}`);
}

export function addUser(user) {
    return http.post('/api/user', user);
}

export function delUser(username) {
    return http.delete(`/api/user/${username}`);
}

export function updateUser(username, user) {
    return http.put(`/api/user/${username}`, user);
}

export function addUserRole(params) {
    return http.post('/api/user/role', params);
}

export function removeUserRole(username, roleName) {
    return http.delete(`/api/user/role/${username}/${roleName}`);
}

export function findModuleByUser(username) {
    return http.get(`/api/user/module/${username}`);
}

export function addUserModule(params) {
    return http.post('/api/user/module', params);
}

export function delUserModule(username, moduleStr) {
    return http.delete(`/api/user/module/${username}/${moduleStr}`);
}

// ------------------------------------------------------------------------------------------- 权限管理

export function findPermission(params) {
    return http.get(`/api/permission?${stringify(params)}`);
}

export function getPermission(permission) {
    return http.get(`/api/permission/${permission}`);
}

export function addPermission(permission) {
    return http.post('/api/permission', permission);
}

export function delPermission(permission) {
    return http.delete(`/api/permission/${permission}`);
}

export function updatePermission(permissionStr, permission) {
    return http.put(`/api/permission/${permissionStr}`, permission);
}

export function findPermissionByRole(params) {
    return http.get(`/api/permission/role?${stringify(params)}`);
}

// ------------------------------------------------------------------------------------------- 系统管理

export function getAllModule() {
    return http.get('/api/module');
}

// ------------------------------------------------------------------------------------------- 角色管理

export function findRole(params) {
    return http.get(`/api/role?${stringify(params, { serializeDate: date => moment(date).format(dateFormat) })}`);
}

export function getRole(name) {
    return http.get(`/api/role/${name}`);
}

export function addRole(role) {
    return http.post('/api/role', role);
}

export function delRole(name) {
    return http.delete(`/api/role/${name}`);
}

export function updateRole(name, role) {
    return http.put(`/api/role/${name}`, role);
}

export function addRolePermission(params) {
    return http.post('/api/role/permission', params);
}

export function removeRolePermission(roleName, permission) {
    return http.delete(`/api/role/permission/${roleName}/${permission}`);
}

export function findRoleByUser(params) {
    return http.get(`/api/role/user?${stringify(params)}`);
}

// ------------------------------------------------------------------------------------------- 角色管理

export function findUserAllRole(username) {
    return http.get(`/api/auth/${username}/role`);
}

export function findUserAllPermission(username) {
    return http.get(`/api/auth/${username}/permission`);
}
